const user = require('../db/models/user.js');
const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken');
const jwtConfig = require('../jwt_config/index.js')
const mongoose = require('mongoose');

// 注册
exports.register = async ( req, res )=> {
  const resgisinfo = req.body;
  if(!resgisinfo.account || !resgisinfo.password) {
    res.status(404).send({ code: 404, message: "账号或密码不能为空" });
    return;
  }
  try{
    const judgeOneAccount  = await user.find({ account: resgisinfo.account })
    if(judgeOneAccount.length !== 0){
      return res.status(404).send({code: 404, message: "账号已被注册"});
      return;
    }
    const createRes =  await user.create({ ...resgisinfo, status: resgisinfo.status || 0, password: bcrypt.hashSync(resgisinfo.password, 10) });
    res.status(200).send({ code: 200, message: "success" });
  }catch(e){
    res.status(500).send({ code: 500 , message: e.message });
  }
}

// 登录
exports.login = async ( req, res )=> {
  const loginInfo = req.body;
  if(!loginInfo.account || !loginInfo.password){
    res.status(404).send({ code: 404, message: "账号或密码不能为空" });
    return;
  }
  try{
    const findRes =  await user.find({ account: loginInfo.account });
    if(findRes.length === 0){
      res.status(404).send({ code: 404, message: "账号未注册" });
      return;
    }
    if(loginInfo.status === 1){
      res.status(404).send({ code: 404, message: "账号已冻结" });
      return;
    }
    if(!bcrypt.compareSync(loginInfo.password, findRes[0].password)) {
      res.status(404).send({ code: 404, message: "登陆失败" });
      return;
    }
    const userInit = {...findRes[0],account: "",password: "",identify: null,name: "",sex: "",status: null,create_time: "",update_time: ""};
    const tokenStr = jwt.sign(userInit, jwtConfig.jwtSecretKey, { expiresIn: '7h' })
    const { id, account, identify, name, sex, status } = findRes[0];
    res.status(200).send({ code: 200, message: "登陆成功", token: "Bearer" + tokenStr, userData: { id, account, identify, name, sex, status } });
  }catch(e){
    res.status(500).send({ code: 500 , message: e.message });
  }
}

// 修改用户信息
exports.update = async ( req, res )=> {
  const editInfo = req.body;
  if(!editInfo.id){
    res.status(404).send({ code: 404, message: "id不能为空" });
    return;
  }
  if(editInfo.name === ""){
    res.status(404).send({ code: 404, message: "姓名不能为空" });
    return;
  }
  try{
    const findRes = await user.find({ _id: editInfo.id });
    if(findRes.length === 0){
      res.status(404).send({ code: 404, message: "修改失败" });
      return;
    }
    let updateOptions = { $set: { name: editInfo.name, sex: editInfo.sex } };
    if(editInfo.password === "") {
      await user.updateOne({ _id: editInfo.id }, updateOptions);
    }
    else {
      updateOptions.$set.password = bcrypt.hashSync(editInfo.password, 10);
      await user.updateOne({ _id: editInfo.id }, updateOptions);
    }
    res.status(200).send({ code: 200, message: "修改成功" });
  }catch(e){
    res.status(500).send({ code: 500 , message: e.message });
  }
}

// 修改密码
exports.changePassword = async ( req, res )=> {
  try{
    const passwordInfo = req.body;
    const isObjectId = mongoose.Types.ObjectId.isValid(passwordInfo.id);
    if(!isObjectId){
      res.status(404).send({ code: 404, message: "Id不能为空" });
      return;
    }
    if(!passwordInfo.oldPassword || !passwordInfo.newPassword || !passwordInfo.secondPassword){
      res.status(404).send({ code: 404, message: "账号或密码不能为空" });
      return;
    }
    if(passwordInfo.newPassword !== passwordInfo.secondPassword){
      res.status(404).send({ code: 404, message: "两次输入密码不一致" });
      return;
    }
    const findRes = await user.find({ _id: passwordInfo.id });
    if(findRes.length === 0){
      res.status(404).send({ code: 404, message: "null" });
      return;
    }
    if(!bcrypt.compareSync(passwordInfo.oldPassword, findRes[0].password)) {
      res.status(404).send({ code: 404, message: "修改失败" });
      return;
    }
    await user.updateOne({ _id: passwordInfo.id }, { $set: { password: bcrypt.hashSync(passwordInfo.newPassword, 10)}});
    res.status(200).send({ code: 200, message: "修改成功" });
  }catch(e){
    res.status(500).send({ code: 500 , message: e.message });
  }
}